package com.uphone.kingmall.login;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.tencent.connect.common.Constants;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.base.BaseUiListener;
import com.uphone.kingmall.bean.LoginBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.main.MainActivity;
import com.uphone.kingmall.utils.ActivityCollector;
import com.uphone.kingmall.utils.AppUtil;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.PhoneFormatCheckUtils;
import com.uphone.kingmall.utils.SharedPreferencesHelper;
import com.uphone.kingmall.utils.StringUtils;
import com.uphone.kingmall.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 王雨
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2018/07/31
 *     desc   :登录
 *     version: 1.0
 * </pre>
 */
public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.iv_qq)
    ImageView ivQq;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.iv_wx)
    ImageView ivWx;
    private IUiListener loginListener;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_login;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
//        GlideUtils.getInstance().loadCircleImage(this, R.mipmap.icon, ivHead);
    }


    @OnClick({R.id.btn_login, R.id.tv_forget_pwd, R.id.tv_register, R.id.iv_qq, R.id.iv_wx})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.tv_forget_pwd:
                openActivity(ForgetPwdActivity.class);
                break;
            case R.id.tv_register:
                openActivity(RegisterActivity.class);
                break;
            case R.id.iv_qq:
                loginQQ();
                break;
            case R.id.iv_wx:
                loginWchat();
                break;
        }
    }

    private void loginWchat() {
        try {
            AppUtil.checkWchat(this, new AppUtil.AppCheckCallback() {
                @Override
                public void callBack() {
                    final SendAuth.Req req = new SendAuth.Req();
                    req.scope = "snsapi_userinfo";
                    req.state = "wechat_sdk_demo_test";
                    MyApplication.api.sendReq(req);
                }
            });
        } catch (Exception e) {
            LogUtils.e("微信登录异常:" + e.toString());
            e.printStackTrace();
        }
    }


    private void loginQQ() {
        AppUtil.checkQQ(this, new AppUtil.AppCheckCallback() {
            @Override
            public void callBack() {
                if (loginListener == null) {
                    loginListener = new BaseUiListener(LoginActivity.this, BaseUiListener.TYPE_LOGIN);
                }
                MyApplication.qqTencent.login(LoginActivity.this, "all", loginListener);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQUEST_LOGIN ||
                requestCode == Constants.REQUEST_APPBAR) {
            if (loginListener != null) {
                Tencent.onActivityResultData(requestCode, resultCode, data, loginListener);
            }
        }

    }

    private void login() {

        if (TextUtils.isEmpty(etPhone.getText().toString()) || TextUtils.isEmpty(etPwd.getText().toString())) {
            showShortToast("请将数据填写完整");
            return;
        }
        if (!PhoneFormatCheckUtils.isPhoneLegal(etPhone.getText().toString())) {
            showShortToast("手机号不正确");
            return;
        }
        if (etPwd.getText().toString().trim().length() < 6 || etPwd.getText().toString().trim().length() > 20) {
            showShortToast("请将密码限制在6-20位之间");
            return;
        }
        if (!StringUtils.isLetterDigit(etPwd.getText().toString().trim())) {
            showShortToast("密码格式不正确");
            return;
        }


        HttpParams params = new HttpParams();
        params.put("loginname", etPhone.getText().toString().trim());
        params.put("password", etPwd.getText().toString().trim());
        OkGoUtils.progressRequest(MyUrl.loginByPass, LoginActivity.this, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String response, int code) {
                LoginBean bean = GsonUtils.getGson().fromJson(response, LoginBean.class);
                if (bean.getUser() != null) {
                    LoginBean.UserBean user = bean.getUser();
                    //设置极光推送别名
                    SharedPreferencesHelper.saveToken(user.getToken());
                    SharedPreferencesHelper.saveUserName(user.getUserName());
                    SharedPreferencesHelper.saveUserPhone(user.getUserMobile());
                    SharedPreferencesHelper.saveUserId(user.getUserId());
                    SharedPreferencesHelper.saveUserImg(user.getUserPhoto());
                    KeyboardUtils.hideSoftInput(LoginActivity.this);
//                    JpushUtil.setTagAndAlias(LoginActivity.this, user.getUserId());
//                    CommonUtil.loginRY();
                }
                if (bean.getCode() == 0) {
                    ToastUtils.showShortToast(LoginActivity.this, "登录成功！");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ActivityCollector.removeAll(MainActivity.class);
                            finish();
                        }
                    }, 500);
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }


}
